﻿using Microsoft.Win32;
using OAZCSharpSdk;
using OAZCSharpSdk.OAZ;
using OAZCSharpSdk.OAZ.Plugin;
using OAZCSharpSdk.OAZ.Plugin.Events;
using OAZCSharpSdk.OAZ.Plugin.Extensions;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;

public static class Exports
{
    private static OAZPlugin plugin { get; set; }

    static void AutoGenerateDumpWhenCrash()
    {
        try
        {
            var outputDmpPath = AppDomain.CurrentDomain.BaseDirectory + "Dump";
            if (!Directory.Exists(outputDmpPath))
            {
                Directory.CreateDirectory(outputDmpPath);
            }
            var fileName = System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName;
            var processName = fileName.Substring(fileName.LastIndexOf('\\') + 1);
            string regPath = @"SOFTWARE\Microsoft\Windows\Windows Error Reporting\LocalDumps\" + processName;
            var reg = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry64);
            var subKey = reg.CreateSubKey(regPath);
            subKey.SetValue("DumpCount", 1, RegistryValueKind.DWord);
            subKey.SetValue("DumpFolder", outputDmpPath, RegistryValueKind.ExpandString);
            subKey.SetValue("DumpType", 1, RegistryValueKind.DWord);
        }
        catch (Exception ex)
        {
            return;
        }
    }

    [DllExport]
    public static string AppInfo()
    {
        try
        {
            AutoGenerateDumpWhenCrash();
            plugin = Program.Main(null);
            var appObj = new
            {
                name = plugin.Name,
                author = plugin.Author,
                description = plugin.Description,
                version = plugin.Version,
                skey = plugin.Skey,
                sdk = plugin.Sdk,
            };
            return appObj.JsonSerialize();
        }
        catch (Exception ex)
        {
            File.WriteAllText(AppDomain.CurrentDomain.BaseDirectory + "\\" + plugin.Name + ".log", String.Format("[{0}][{1}] {2}", plugin.Name, "AppInfo", ex.ToString()));
            return "";
        }
    }

    [DllExport]
    public static int Load()
    {
        try
        {
            plugin.InvokeLoad(null, new OAZCSharpSdk.OAZ.Plugin.Events.PluginLoadEventArgs());
            return 0;
        }
        catch (Exception ex)
        {
            OAZCSharpSdk.OAZ.OAZ.Api_Output(String.Format("[{0}][{1}] {2}", plugin.Name, "Load", ex.ToString()));
            return 0;
        }
    }

    [DllExport]
    public static string Enable()
    {
        try
        {
            OAZCSharpSdk.OAZ.Plugin.Events.PluginEnableArgs e = new OAZCSharpSdk.OAZ.Plugin.Events.PluginEnableArgs();
            plugin.InvokeEnable(null, e);
            return e.CanelReason;
        }
        catch (Exception ex)
        {
            OAZCSharpSdk.OAZ.OAZ.Api_Output(String.Format("[{0}][{1}] {2}", plugin.Name, "Enable", ex.ToString()));
            return "";
        }
    }

    [DllExport]
    public static int Disable()
    {
        try
        {
            plugin.InvokeDisable(null, new OAZCSharpSdk.OAZ.Plugin.Events.PluginDisableEventArgs());
            return 0;
        }
        catch (Exception ex)
        {
            OAZCSharpSdk.OAZ.OAZ.Api_Output(String.Format("[{0}][{1}] {2}", plugin.Name, "Disable", ex.ToString()));
            return 0;
        }
    }

    [DllExport]
    public static int Set()
    {
        try
        {
            plugin.InvokeSet(null, new OAZCSharpSdk.OAZ.Plugin.Events.PluginSetEventArgs());
            return 0;
        }
        catch (Exception ex)
        {
            OAZCSharpSdk.OAZ.OAZ.Api_Output(String.Format("[{0}][{1}] {2}", plugin.Name, "Set", ex.ToString()));
            return 0;
        }
    }

    [DllExport]
    public static int End()
    {
        try
        {
            plugin.InvokeEnd(null, new OAZCSharpSdk.OAZ.Plugin.Events.PluginEndEventArgs());
            return 0;
        }
        catch (Exception ex)
        {
            OAZCSharpSdk.OAZ.OAZ.Api_Output(String.Format("[{0}][{1}] {2}", plugin.Name, "End", ex.ToString()));
            return 0;
        }
    }

    [DllExport]
    public static int Event(IntPtr ptr)
    {
        try
        {
            //该事件会导致蹦溃，暂未查明
            return 1;
            //string str = ptr.GetString();
            //var msg = str.JsonDeserialize<OAZCSharpSdk.OAZ.Plugin.Model.NotifyInfo>();
            //long RobotQQ = long.Parse(msg.fromMyUin);
            //var e = new PluginNotifyEventArgs(RobotQQ);
            //plugin.InvokeNotify(RobotQQ, e);
            //if (e.Block)
            //{
            //    return 0;
            //}
            //switch (msg.type)
            //{
            //    case OAZ.Event_Notify_Friend_Deleted:
            //        plugin.InvokeNotifyFriendDeleted(RobotQQ, e);
            //        break;
            //    case OAZ.Event_Notify_Friend_New:
            //        plugin.InvokeNotifyFriendNew(RobotQQ, e);
            //        break;
            //    case OAZ.Event_Notify_Friend_Reqeust:
            //        plugin.InvokeNotifyFriendRequest(RobotQQ, e);
            //        break;
            //    case OAZ.Event_Notify_Friend_Request_Accpet:
            //        plugin.InvokeNotifyFriendRequestAccpet(RobotQQ, e);
            //        break;
            //    case OAZ.Event_Notify_Friend_Request_Refuse:
            //        plugin.InvokeNotifyFriendRequestRefuse(RobotQQ, e);
            //        break;
            //    default:
            //        break;
            //}
            //OAZCSharpSdk.OAZ.OAZ.Api_Output("Event=>" + ptr.GetString());
            return 0;
        }
        catch (Exception ex)
        {
            OAZCSharpSdk.OAZ.OAZ.Api_Output(String.Format("[{0}][{1}] {2}", plugin.Name, "Event", ex.ToString()));
            return 0;
        }
    }

    [DllExport]
    public static int PrivateMsg(IntPtr ptr)
    {
        try
        {
            //OAZCSharpSdk.OAZ.OAZ.Api_Output("PrivateMsg=>" + ptr.GetString());
            return 0;
        }
        catch (Exception ex)
        {
            OAZCSharpSdk.OAZ.OAZ.Api_Output(String.Format("[{0}][{1}] {2}", plugin.Name, "PrivateMsg", ex.ToString()));
            return 0;
        }
    }

    [DllExport]
    public static int GroupMsg(IntPtr ptr)
    {
        try
        {
            var msg = ptr.GetString().JsonDeserialize<OAZCSharpSdk.OAZ.Plugin.Model.GroupMsg>();
            long RobotQQ = long.Parse(msg.myUin);
            long GroupQQ = long.Parse(msg.fromGroup);
            OAZCSharpSdk.OAZ.Plugin.Cache.GroupCache.InitializeGroupManagers(RobotQQ, GroupQQ);
            plugin.InvokeGroupMessage(msg.myUin, new OAZCSharpSdk.OAZ.Plugin.Events.PluginGroupMessageEventArgs(RobotQQ)
            {
                Content = msg.message_content,
                GroupQQ = GroupQQ,
                GroupName = msg.groupName,
                QQ = long.Parse(msg.fromUin),
                CardName = msg.fromCard,
            });
            return 0;
        }
        catch (Exception ex)
        {
            OAZCSharpSdk.OAZ.OAZ.Api_Output(String.Format("[{0}][{1}] {2}", plugin.Name, "GroupMsg", ex.ToString()));
            return 0;
        }
    }

    [DllExport]
    public static int GulidMsg(IntPtr ptr)
    {
        try
        {
            //OAZCSharpSdk.OAZ.OAZ.Api_Output("GulidMsg=>" + ptr.GetString());
            return 0;
        }
        catch (Exception ex)
        {
            OAZCSharpSdk.OAZ.OAZ.Api_Output(String.Format("[{0}][{1}] {2}", plugin.Name, "GulidMsg", ex.ToString()));
            return 0;
        }
    }
}
